package com.haocang.szgm.ui.floodpoint.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

import com.haocang.szgm.utils.config.Config;
import com.haocang.szgm.utils.tools.GetCommonParameters;
import com.haocang.szgm.utils.tools.MyTextUtils;

import net.tsz.afinal.FinalActivity;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 创建时间：2019/9/19
 * 编 写 人：ShenC
 * 功能描述：
 */

public class ShotShareUtil {


    /**
     * 获取指定Activity的截屏
     */
    public static boolean activityScreenShot(FinalActivity activity, String pngName) {


        try {
            // View是你需要截图的View
            View view = activity.getWindow().getDecorView();
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            Bitmap bitmap = view.getDrawingCache();

            // 获取状态栏高度
            Rect frame = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            int statusBarHeight = frame.top;

            // 获取屏幕长和高
            int width = activity.getWindowManager().getDefaultDisplay().getWidth();
            int height = activity.getWindowManager().getDefaultDisplay().getHeight();

            // 去掉标题栏
            Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight);
            view.destroyDrawingCache();

            screenShot(activity, b, pngName);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取scrollview的截屏
     */
    public static boolean scrollViewScreenShot(FinalActivity context, ScrollView scrollView, String pngName) {
        try {
            int h = 0;
            Bitmap bitmap = null;
            for (int i = 0; i < scrollView.getChildCount(); i++) {
                h += scrollView.getChildAt(i).getHeight();
                scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
            }
            bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565);
            final Canvas canvas = new Canvas(bitmap);
            scrollView.draw(canvas);

            screenShot(context, bitmap, pngName);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 获取截屏
     **/
    private static void screenShot(FinalActivity contex, Bitmap bitmap, String pngName) {
        String imagePath;
        if (bitmap != null) {
            try {
                // 图片文件路径
                imagePath = Environment.getExternalStorageDirectory().getPath()
                        + "/" + Config.APP_Code + "/report/" + pngName + ".png";
                File file = new File(imagePath);
                FileOutputStream os = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
                ShareImage(contex, imagePath);
            } catch (Exception e) {
                Log.d("shot", "====screenshot:error====" + e.getMessage());
            }

        }


    }


    /**
     * 分享
     **/
    private static void ShareImage(FinalActivity context, String imagePath) {
        if (imagePath != null) {
            Intent intent = new Intent(Intent.ACTION_SEND); // 启动分享发送的属性
            Uri urii = GetCommonParameters.GetFileProviderUri(context, imagePath);
            intent.putExtra(Intent.EXTRA_STREAM, urii);// 分享的内容
            intent.setType("image/*");// 分享发送的数据类型
            Intent chooser = Intent.createChooser(intent, "Share screen shot");
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(chooser);
            }
        } else {
            Toast.makeText(context, "先截屏，再分享", Toast.LENGTH_SHORT).show();
        }

    }

    public static void sharePic(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(activity.getContentResolver(),
                takeScreenShot(activity), null, null));//将截图bitmap存系统相册
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(Intent.createChooser(intent, ""));
    }

    public static Bitmap takeScreenShot(Activity pActivity) {
        View view = pActivity.getWindow().getDecorView();
        // 设置是否可以进行绘图缓存
        view.setDrawingCacheEnabled(true);
        // 如果绘图缓存无法，强制构建绘图缓存
        view.buildDrawingCache();
        // 返回这个缓存视图
        Bitmap bitmap = view.getDrawingCache();
        // 获取状态栏高度
        Rect frame = new Rect();
        // 测量屏幕宽和高
        view.getWindowVisibleDisplayFrame(frame);
        int stautsHeight = frame.top;
        Point point = new Point();
        pActivity.getWindowManager().getDefaultDisplay().getSize(point);
        int width = point.x;
        int height = point.y;
        // 根据坐标点和需要的宽和高创建bitmap
        bitmap = Bitmap.createBitmap(bitmap, 0, stautsHeight, width, height - stautsHeight);
        return bitmap;
    }

}
